#!/usr/bin/env python
# -*- coding:utf-8 -*-

# 画结果轨迹图
import csv
from numpy import *
import matplotlib.pyplot as plt

riders = []
for i in range(3100):
    riders.append('D' + str(i + 1).zfill(4))
# print riders
# print len(riders)

reader = csv.reader(file('../output/result_o2o.csv', 'rU'))
# 骑手轨迹
routs = []
# 所有骑手轨迹
allrouts = []
i = 0
for line in reader:
    if line[0] == riders[i]:  # 骑手没变
        routs.append([line[1], line[2], line[3], line[4], line[5]])
    else:
        allrouts.extend([routs])
        i += 1  # 顺延骑手
        routs = []
        routs.append([line[1], line[2], line[3], line[4], line[5]])

allrouts.extend([routs])
# print routs
# print allrouts

# 网点坐标获取
site = {}
reader = csv.reader(file('../info/new_1.csv', 'rU'))
for line in reader:
    site[line[0]] = [line[1], line[2]]
siteln = []
sitela = []
for s in site:
    siteln.append(site[s][0])
    sitela.append(site[s][1])

# 配送点坐标获取
spot = {}
reader = csv.reader(file('../info/new_2.csv', 'rU'))
for line in reader:
    spot[line[0]] = [line[1], line[2]]
spotln = []
spotla = []
for s in spot:
    spotln.append(spot[s][0])
    spotla.append(spot[s][1])

# 商户坐标获取
shop = {}
reader = csv.reader(file('../info/new_3.csv', 'rU'))
for line in reader:
    shop[line[0]] = [line[1], line[2]]
shopln = []
shopla = []
for s in shop:
    shopln.append(shop[s][0])
    shopla.append(shop[s][1])

fig = plt.figure()
# 可以画多图 分割成1行1列，图像画在从左到右从上到下的第1块
# ax = fig.add_subplot(111)
plt.title('Maping Plot')
plt.xlabel('Longitude')
plt.ylabel('Latitude')

# 画散列点
# plt.scatter(spotln, spotla, c='blue', marker='+', label='spot(9214)')     # 配送点
plt.scatter(siteln, sitela, c='red', marker='o', label='site(124)')  # 网点
# for point in site.keys():
#     # if point == 'A020':
#     plt.annotate(point, xy=(site[point][0], site[point][1]),
#                  xytext=(mat(site[point][0]) + random.random()*0.1-0.05,mat(site[point][1])+random.random()*0.1-0.05),
#                  arrowprops=dict(arrowstyle="->", connectionstyle="arc3"))

plt.scatter(shopln, shopla, c='black', marker='*', label='shop(598)')  # 商户
# 图示位置 右上
plt.legend(loc='upper right')

# reader = csv.reader(file('meansdataonemean.csv','rb'))
# for item in reader:
#     c = item
# for p in c:
#     if 'A' in p:
#         plt.scatter(site[p][0],site[p][1],c='yellow',marker='o')
#     if 'S' in p:
#         plt.scatter(shop[p][0],shop[p][1],c='yellow', marker='o')

x = []
y = []
allrouts.sort(key=lambda routs: len(routs))

for l in range(len(allrouts)):
    routs = [m[0] for m in allrouts[l]]
    starttime = [m[1] for m in allrouts[l]]
    endtime = [m[2] for m in allrouts[l]]
    order = [m[4] for m in allrouts[l]]
    cout = [m[3] for m in allrouts[l]]
    x = []
    y = []
    if l >= 3100:
        break
    if l < 0:
        continue
    for k in range(len(routs)):
        length = len(routs)
        i = routs[k]
        if 'A' in i:
            x.append(site[i][0])
            y.append(site[i][1])
        if 'B' in i:
            x.append(spot[i][0])
            y.append(spot[i][1])
        if 'S' in i:
            x.append(shop[i][0])
            y.append(shop[i][1])

            # if k == length - 1 and routs[-1] != routs[-2]:
            #     plt.annotate(riders[l]+' '+order[-1]+' '+starttime[-1], xy = (x[k], y[k]),     # 加上配送员id
            #                  xytext=(mat(x[k])+random.random()*0.01-0.005,mat(y[k])+random.random()*0.01-0.005),
            #                  arrowprops=dict(arrowstyle="->",connectionstyle="arc3"))
            # plt.annotate(routs[-1], xy=(x[k], y[k]),                                       # 加上路线的最后一个配送点id
            #              xytext=(mat(x[k]) +random.random()*0.01-0.005, mat(y[k]) +random.random()*0.01-0.005),
            #              arrowprops=dict(arrowstyle="->", connectionstyle="arc3"))
    # if routs[0] == 'A020':
    # plt.plot(x, y)
    # plt.plot(x[:5], y[:5])
    plt.plot(x[-2:], y[-2:])
plt.show()
